﻿module Supernova {

    export class Notices {

        private static MAX_HELD_MESSAGES = 32;

        private static MAX_RENDERED_MESSAGES = 8;

        private static messages: Array<Message> = [];

        private static toasts: Array<Toast> = [];

        public static clearMessages() {
            Notices.messages = [];
        }

        public static addMessage(type: MessageType, text: string) {
            var fillStyle = '#fff';
            if (type == MessageType.fatal) {
                fillStyle = '#f00';
            }
            if (type == MessageType.fromServer || type == MessageType.chat) {
                fillStyle = type == MessageType.fromServer ? '#22f' : '#ff2';
                Sounds.play(Sounds.items.alert);
            }
            var message = new Message(fillStyle, text);
            if (type == MessageType.chat) {
                message.framesRemaining = -1;
            }
            Notices.messages.push(message);
            if (Notices.messages.length > Notices.MAX_HELD_MESSAGES) {
                Notices.messages.shift();
            }
        }

        public static renderMessages() {
            context.textAlign = 'left';
            context.font = '18px Verdana';

            // How many messages are going to be rendered?
            var firstIndex = Math.max(0, Notices.messages.length - Notices.MAX_RENDERED_MESSAGES);
            var count = Notices.messages.length - firstIndex;

            // Where should we start rendering?
            var x = 0;
            var y = 0;
            if (activeUI == lobbyUI) {
                var chatTextBox = UI.getControlByName(UI.root, 'chatTextBox');
                x = chatTextBox.computed.left;
                y = chatTextBox.computed.top - 20 * count;
            } else if (activeUI == gameUI) {
                x = gameUI.bottomPanel.computed.left;
                y = gameUI.bottomPanel.computed.top - 20 * count;
            } else {
                y = canvas.height - 20 * count;
            }

            // Draw each message in turn
            for (var i = firstIndex; i < Notices.messages.length; i++) {
                var message = Notices.messages[i];
                context.fillStyle = message.fillStyle;
                context.fillText(message.text, x + 8, y - 8);
                if (message.framesRemaining > 0) message.framesRemaining--;
                y += 20;
            }

            // Remove old messages that have expired
            for (var i = 0; i < Notices.messages.length; i++) {
                var message = Notices.messages[i];
                if (message.framesRemaining == 0) {
                    Notices.messages.splice(i, 1);
                    i--;
                }
            }
        }

        public static clearToasts() {
            Notices.toasts = [];
        }

        public static addToast(x: number, y: number, text: string) {
            Notices.toasts.push(new Toast(x, y, text));
        }

        public static renderToasts() {
            context.textBaseline = 'middle';
            for (var i = Notices.toasts.length - 1; i >= 0; i--) {
                var toast = Notices.toasts[i];
                var l = worldToScreen(toast.x, toast.y);
                context.textAlign = 'center';
                context.font = '16px Calibri';
                var width = context.measureText(toast.text).width;
                context.fillStyle = '#fff';
                context.fillRect(l.x - width / 2 - 2, l.y - 12 - toast.life * toast.speed, width + 4, 24);
                context.fillStyle = '#000';
                context.fillText(toast.text, l.x, l.y - toast.life * toast.speed);
                toast.life += 1;
                if (toast.life > toast.maxLife) {
                    Notices.toasts.splice(i, 1);
                }
            }
        }

    }

}